<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>download.js文件下载快速使用入门</title>

</head>
<body>

<button type="button" style="font-size: 18px" id="download1">下载文本内容</button>
<button type="button" style="font-size: 18px" id="download2">下载Blob类型</button>
<button type="button" style="font-size: 18px" id="download3">URL下载普通文件</button>
<button type="button" style="font-size: 18px" id="download4">下载页面源码</button>
<button type="button" style="font-size: 18px" id="download5">URL下载图片文件</button>
<button type="button" style="font-size: 18px" id="download6">URL下载视频文件</button>

</body>

<script src="https://cdn.bootcdn.net/ajax/libs/downloadjs/1.4.8/download.js"></script>
<script type="text/javascript">
    window.onload = function () {
        document.getElementById("download1").addEventListener("click", function () {
            // 文本内容下载
            download("hello download string文本", "string文本.txt", "text/plain");
        });
        document.getElementById("download2").addEventListener("click", function () {
            // blob 内容类型下载
            download(new Blob(["hello Blob 类型"]), "dlTextBlob.txt", "text/plain");
            download(new Blob(["hello world".bold()]), "dlHtmlBlob.html", "text/html");
        });
        document.getElementById("download3").addEventListener("click", function () {
            // 通过 url 地址下载。可以是相对路径或者绝对路径，文件名称与格式自动获取
            download("https://cdn.bootcdn.net/ajax/libs/downloadjs/1.4.8/download.js");
        });
        document.getElementById("download4").addEventListener("click", function () {
            // 下载HTML页面源码
            download(document.documentElement.outerHTML, "dlHTML页面源码.html", "text/html");
        });

        document.getElementById("download5").addEventListener("click", function () {
            // 通过url下载图片。可以是相对路径或者绝对路径，文件名称与格式自动获取。
            // 名称无法自动获取时，默认为 download
            download("https://nimg.ws.126.net/?url=http%3A%2F%2Fdingyue.ws.126.net%2F2023%2F0125%2F49be0a0bj00rp1sop002qc000hs00psm.jpg&thumbnail=660x2147483647&quality=80&type=jpg");
        });

        document.getElementById("download6").addEventListener("click", function () {
            // ajax 异步请求，后台返回流进行下载。
            var request = new XMLHttpRequest();
            request.open("GET", "/fileServer/generateScript", true);
            request.responseType = "application/octet-stream";
            request.onload = function (e) {
                download(e.target.response, "a.sql", "application/octet-stream");
            };
            request.send();
        });
    };
</script>

</html>